// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include "AIPlayer.h"
#include "Planet.h"
#include "GameManager.h"

using namespace std;

AIPlayer::AIPlayer(void)
{
	_stop = false;

	for (int i = 0; i < 100; i++)
		_rules[i] = NULL;

	ifstream fin("../script.txt");
	string s, str;
	int i = 0;

	while( getline(fin, s) ) {
		istringstream iss(s);
		string token;
		int value;
		_rules[i] = new struct Rule();

		iss >> token;
		if (token.compare("if") == 0) {
			iss >> token;
			if (token.compare("gold") == 0)
				_rules[i]->cond.op1 = KT_GOLD;
			else if ((token.compare("time") == 0))
				_rules[i]->cond.op1 = KT_TIME;
			else if ((token.compare("cons") == 0))
				_rules[i]->cond.op1 = KT_CONSTRUCT;

			iss >> token;
			if (token.compare(">") == 0)
				_rules[i]->cond.oper = OT_GREATTHAN;
			else if (token.compare("<") == 0)
				_rules[i]->cond.oper = OT_SMALLERTHAN;
			else if (token.compare("=") == 0 || token.compare("==") == 0)
				_rules[i]->cond.oper = OT_EQUAL;
			else if (token.compare("%") == 0)
				_rules[i]->cond.oper = OT_EVERY;

			iss >> value;
			_rules[i]->cond.op2 = value;

			iss >> token;
			if (token.compare("build") == 0) {
				_rules[i]->act.action = AT_BUILD;
				iss >> value;
				_rules[i]->act.value = value;
			}
			else if (token.compare("settarget") == 0) {
				_rules[i]->act.action = AT_SETTARGET;
				iss >> token;
				iss >> token;
			}
			else if (token.compare("setland") == 0) {
				_rules[i]->act.action = AT_SETLAND;
				iss >> value;
				_rules[i]->act.value = value;
			}
		}
		i++;
	}

}

AIPlayer::~AIPlayer(void)
{
}


void AIPlayer::update(float timestep, float totalTime)
{
	Player::update(timestep, totalTime);

	if (_stop)
		return;

	int value;
	bool cond = false;

	for (int i = 0; _rules[i]; i++) {
		switch (_rules[i]->cond.op1) {
			case KT_CONSTRUCT:
				value = (*(getPlanets().begin()))->getConstructionPoint();
				switch (_rules[i]->cond.oper) {
					case OT_SMALLERTHAN:
						cond = value < _rules[i]->cond.op2;
						break;
					case OT_GREATTHAN:
						cond = value > _rules[i]->cond.op2;
						break;
					case OT_EQUAL:
						cond = value == _rules[i]->cond.op2;
						break;
				}

				break;
			case KT_TIME:
				value = (int)totalTime;
				switch (_rules[i]->cond.oper) {
					case OT_EVERY:
						cond = value % _rules[i]->cond.op2 == _rules[i]->cond.op2 - 1;
						break;
				}

				break;
		}

		if (cond) {
			switch (_rules[i]->act.action) {
				case AT_BUILD:
					if (*(getPlanets().begin()) == 0)
						break;
					switch (_rules[i]->act.value) {
						case 1:
							(*(getPlanets().begin()))->orderSpaceship(1, ST_YATAI_VIII);
							break;
						case 2:
							(*(getPlanets().begin()))->orderSpaceship(1, ST_JONTH);
							break;
						case 3:
							(*(getPlanets().begin()))->orderSpaceship(1, ST_CYSUS);
							break;
						case 4:
							(*(getPlanets().begin()))->orderSpaceship(1, ST_METHI);
							break;
					}
					break;
				case AT_SETTARGET:
					for (set<Spaceship*>::const_iterator i = getSpaceships().begin(); i != getSpaceships().end(); ++i) {
						SpaceObject* so = GameManager::getInstance()->findNearestEnemy(*i);
						if (so)
							(*i)->setTarget(so);
					}
					break;
				case AT_SETLAND:
					break;
			}
		}
	}
	//(*(getPlanets().begin()))->orderSpaceship(1, ST_YATAI_VIII);
	if (value % 2 == 0)
		(*(getPlanets().begin()))->setConstructionPoint((*(getPlanets().begin()))->getConstructionPoint() + 1);
}

void AIPlayer::stop()
{
	_stop = true;
}